﻿using Newtonsoft.Json.Linq;
using System.Collections.Generic;

namespace Sidvall.Serialization
{
    public static class JObjectExtension
    {
        #region ValueToString

        public static string ValueToString(this JObject value, string propertyName)
        {
            JToken item;
            if (value != null && value.TryGetValue(propertyName, System.StringComparison.OrdinalIgnoreCase, out item))
                return item.ToString();
            return null;
        }

        #endregion
        #region ValueToStrings

        public static IEnumerable<string> ValueToStrings(this JObject value, string propertyName)
        {
            JToken item;

            var items = new List<string>();
            if (value != null && value.TryGetValue(propertyName, System.StringComparison.OrdinalIgnoreCase, out item))
            {
                if (!item.HasValues)
                    return null;
                foreach (var itemValue in item.Values())
                    items.Add(itemValue.ToString());
                return items;
            }
            return null;
        }

        #endregion
    }
}
